//
//  WKFangBiPopView.m
//  XTXOneToOne
//
//  Created by Canny on 2018/11/11.
//  Copyright © 2018年 WK. All rights reserved.
//

#import "WKFangBiPopView.h"


@interface WKFangBiPopView ()<UITextViewDelegate>

/** 弹窗主内容view */
@property (nonatomic,strong) UIView   *contentView;
/** 弹窗标题 */
@property (nonatomic,copy)   NSString *title;
/** 弹窗message */
@property (nonatomic,copy)   NSString *message;
/** message label */
@property (nonatomic,strong) UILabel  *messageLabel;
/** 左边按钮title */
@property (nonatomic,copy)   NSString *leftButtonTitle;
/** 右边按钮title */
@property (nonatomic,copy)   NSString *rightButtonTitle;

@property (nonatomic,strong) UIButton * btn;
@property (nonatomic,assign) BOOL isSelectedXieYi;

@end



@implementation WKFangBiPopView{
    UILabel *label;
}

#pragma mark - 构造方法
/**
 申报异常弹窗的构造方法
 
 @param title 弹窗标题
 @param message 弹窗message
 @param delegate 确定代理方
 @param leftButtonTitle 左边按钮的title
 @param rightButtonTitle 右边按钮的title
 @return 一个申报异常的弹窗
 */
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate leftButtonTitle:(NSString *)leftButtonTitle rightButtonTitle:(NSString *)rightButtonTitle{
    if (self = [super init]) {
        self.title = title;
        self.message = message;
        self.delegate = delegate;
        self.leftButtonTitle = leftButtonTitle;
        self.rightButtonTitle = rightButtonTitle;
        
        // 接收键盘显示隐藏的通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];
        
        // UI搭建
        [self setUpUI];
    }
    return self;
}

#pragma mark - UI搭建
/** UI搭建 */
- (void)setUpUI{
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    [UIView animateWithDuration:0.1 animations:^{
        self.alpha = 1;
    }];
    
    //------- 弹窗主内容 -------//
    self.contentView = [[UIView alloc]init];
    self.contentView.frame = CGRectMake((G_SCREENHEIGHT - 285) / 2, (G_SCREENHEIGHT - 215) / 2, 285, 215);
    self.contentView.center = self.center;
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.cornerRadius = 6;
    
    // 标题
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, self.contentView.width, 22)];
    [self.contentView addSubview:titleLabel];
    titleLabel.font = [UIFont boldSystemFontOfSize:16];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = self.title;
    
    UILabel *subtitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(22, 30, self.contentView.width-44, 60)];
    [self.contentView addSubview:subtitleLabel];
    subtitleLabel.font = [UIFont systemFontOfSize:13];
    subtitleLabel.textAlignment = NSTextAlignmentLeft;
    subtitleLabel.textColor=RGB(100, 100, 100);
    subtitleLabel.text = @"请务必登录网上银行或第三方支付账户确认收到该笔款项。您确认收到了该笔款项吗？";
    subtitleLabel.numberOfLines=0;
    
    
    // 填写异常情况描述的textView
    self.textField = [[UITextField alloc]initWithFrame:CGRectMake(22, subtitleLabel.maxY + 0, self.contentView.width - 44, 40)];
    [self.contentView addSubview:self.textField];
    [self setTextField:self.textField WithPlaceholder:@"请输入资金密码" and:UIKeyboardTypeDefault];
    self.textField.secureTextEntry=YES;
    
    UIView * line = [[UIView alloc]initWithFrame:CGRectMake(22, self.textField.maxY + 1, self.contentView.width - 44, 1)];
    [self.contentView addSubview:line];
    line.backgroundColor = RGB(200, 200, 200);
    
    
    UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(self.textField.x, self.textField.maxY + 15, 20, 20)];
    [self.contentView addSubview:btn];
    [btn setImage:[UIImage imageNamed:@"选择btn-未选中"] forState:UIControlStateNormal];
    _btn=btn;
    [btn addTarget:self action:@selector(selectedXieYi:) forControlEvents:UIControlEventTouchUpInside];
    _isSelectedXieYi = NO;
    
    UILabel *tipLabel = [[UILabel alloc]initWithFrame:CGRectMake(btn.x+30, self.textField.maxY + 10, self.contentView.width-44-30, 30)];
    [self.contentView addSubview:tipLabel];
    tipLabel.textColor=RGB(141, 160, 173);
    tipLabel.font = [UIFont systemFontOfSize:12];
    tipLabel.textAlignment = NSTextAlignmentLeft;
    tipLabel.text = @"我已知晓:需在付款成功后点击下方按钮";
    tipLabel.numberOfLines=0;
    
    // 红色提示label
    UILabel *redLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.textField.x, self.textField.maxY + 60, self.textField.width, 20)];
    [self.contentView addSubview:redLabel];
    redLabel.textColor = [UIColor redColor];
    redLabel.font = [UIFont systemFontOfSize:12];
    redLabel.numberOfLines = 0;
    if ([self.message isEqualToString:@""]) {
        redLabel.text = @"";
    }else{
        redLabel.text = [NSString stringWithFormat:@"（%@）",self.message];
    }
    [redLabel sizeToFit];
    
    //
    UIButton *abnormalButton = [[UIButton alloc]initWithFrame:CGRectMake(self.textField.minX, redLabel.maxY + 5, 100, 40)];
    [self.contentView addSubview:abnormalButton];
    abnormalButton.backgroundColor = RGB(231, 235, 238);
    [abnormalButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [abnormalButton setTitle:@"取消" forState:UIControlStateNormal];
    [abnormalButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
    abnormalButton.layer.cornerRadius = 6;
    [abnormalButton addTarget:self action:@selector(abnormalButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
    //
    UIButton *cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(self.textField.maxX - 100, abnormalButton.minY, 100, 40)];
    [self.contentView addSubview:cancelButton];
    [cancelButton setTitle:@"确定" forState:UIControlStateNormal];
    cancelButton.backgroundColor = RGB(101, 141, 216);
    [cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [cancelButton.titleLabel setFont:[UIFont systemFontOfSize:18]];
    cancelButton.layer.cornerRadius = 6;
    [cancelButton addTarget:self action:@selector(cancelButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
    //------- 调整弹窗高度和中心 -------//
    self.contentView.height = cancelButton.maxY + 10;
    self.contentView.center = self.center;
    
}

#pragma mark - 弹出此弹窗
/** 弹出此弹窗 */
- (void)show{
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self];
}

#pragma mark - 移除此弹窗
/** 移除此弹窗 */
- (void)dismiss{
    [self removeFromSuperview];
}

#pragma mark - 取消
- (void)abnormalButtonClicked{
    if ([self.delegate respondsToSelector:@selector(declareWKFangBiPopView:clickedButtonAtIndex:andPassword:)]) {
        [self.delegate declareWKFangBiPopView:self clickedButtonAtIndex:WKFangBiAlertButtonLeft andPassword:_textField.text];
    }
    [self dismiss];
}

#pragma mark - 确定
- (void)cancelButtonClicked{
    if (!_isSelectedXieYi) {
        [HUD showErrorMessage:@"请同意协议！"];
        return;
    }
    if (self.textField.text.length<1) {
        [HUD  showErrorMessage:@"请输入资金密码！"];
        return;
    }
    if ([self.delegate respondsToSelector:@selector(declareWKFangBiPopView:clickedButtonAtIndex:andPassword:)]) {
        [self.delegate declareWKFangBiPopView:self clickedButtonAtIndex:WKFangBiAlertButtonRight andPassword:_textField.text];
    }
    [self dismiss];
}

#pragma mark - UITextView代理方法
- (void)textViewDidChange:(UITextView *)textView{
    if ([textView.text isEqualToString:@""]) {
        self.messageLabel.hidden = NO;
    }else{
        self.messageLabel.hidden = YES;
    }
    NSString * content = textView.text;
    if (textView.text.length>200) {
        textView.text = [content substringToIndex:200];
    }
}

/**
 *  键盘将要显示
 *
 *  @param notification 通知
 */
-(void)keyboardWillShow:(NSNotification *)notification
{
    // 获取到了键盘frame
    CGRect frame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat keyboardHeight = frame.size.height;
    
    self.contentView.maxY = G_SCREENHEIGHT - keyboardHeight - 10;
}
/**
 *  键盘将要隐藏
 *
 *  @param notification 通知
 */
-(void)keyboardWillHidden:(NSNotification *)notification
{
    // 弹窗回到屏幕正中
    self.contentView.centerY = G_SCREENHEIGHT / 2;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.textField resignFirstResponder];
}

-(void)setTextField:(UITextField*)textField WithPlaceholder:(NSString *)placeholder and:(UIKeyboardType)type  {
    
    
    textField.placeholder=placeholder;
    textField.borderStyle = UITextBorderStyleNone;
    //    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    //    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    textField.backgroundColor=[UIColor clearColor];
    textField.font = [UIFont systemFontOfSize:15];
    textField.textColor = RGB(32, 64, 89);
    textField.textAlignment = NSTextAlignmentLeft;
    //    textField.returnKeyType = UIReturnKeyDefault;
    textField.keyboardType = type;
    [textField setValue:RGB(190, 190, 190) forKeyPath:@"_placeholderLabel.textColor"];
    [textField setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:(UIControlEventEditingChanged)];
    
}

- (void) textFieldDidChange:(id) sender {
    
    UITextField *_field = (UITextField *)sender;
    
    NSLog(@"%@",[_field text]);
    
}

-(void)selectedXieYi:(UIButton *)btn{
    if (_isSelectedXieYi == NO) {
        [_btn setImage:[UIImage imageNamed:@"选择btn-选中"] forState:UIControlStateNormal];
        _isSelectedXieYi=YES;
    }else{
        [_btn setImage:[UIImage imageNamed:@"选择btn-未选中"] forState:UIControlStateNormal];
        _isSelectedXieYi=NO;
    }
}


@end

